function ajax(url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.responseType = "json";
    xhr.onload = function () {
      if (this.status === 200) {
        resolve(this.response);
      } else {
        reject(this.statusText);
      }
    };
    xhr.send();
  });
}

// Promise.resolve(value)：快速的将一个值转换为成功的Promise对象
// Promise.reject(new Error(value))：快速将一个值转换为失败的Promise对象
/* Promise.resolve("foo").then((val) => {
  console.log(val);
});

// 等同于
new Promise((resolve, reject) => {
  resolve("foo");
}) */

// Promise.resolve(promise): 返回原本的Promise对象
// const promise = ajax("./api/urls.json");
// const promise2 = Promise.resolve(promise);
// console.log(process === promise2);

// thenable
// Promise.resolve({
//   then: (onFulfilled, onRejected) => {
//     onFulfilled("foo");
//   },
// }).then((val) => {
//   console.log("🚀 ~ val:", val);
// });
